package com.example.scale;
import org.andengine.engine.Engine;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.extension.physics.box2d.PhysicsConnector;
import org.andengine.extension.physics.box2d.PhysicsFactory;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.util.adt.color.Color;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.FixtureDef;


public class Turret extends AnimatedSprite {

	private VertexBufferObjectManager mvbom;
	//private PhysicsWorld mPhysicsWorld;
	//private Camera mCamera;
	private Engine mEngine;
	private FixtureDef FIXTURE_DEF = PhysicsFactory.createFixtureDef(0, 0, 0);
	

	public Turret(float pX, float pY, VertexBufferObjectManager vbo, Engine engine) {
		super(pX, pY+40, ResourcesManager.getInstance().turret_region, vbo);
		
		mvbom = vbo;
		//mPhysicsWorld = physicsWorld;
		//mCamera = camera;
		mEngine = engine;
		//final Sprite TurretBase = new Sprite(pX-120,
		//		pY, ResourcesManager.getInstance().turret_base_region, vbo);
		//attachChild(TurretBase);
		final int startRotation = 0;
		
		setScale(0.25f);
		setRotation(startRotation);
		
		//int timeFrame = 250;
		//final long[] TURRET_ANIMATE = new long[] {timeFrame, timeFrame};
		animate(new long[] {1000, 600}, 0, 1, true);

		
		//createLaser();
	}
	
	public void createLaser() {
		Laser mLaser = new Laser(getX()-700, getY(), 1000, 3, mvbom, this);
		FixtureDef areaFixtureDef = PhysicsFactory.createFixtureDef(0, 0, 0);
		areaFixtureDef.isSensor = true;
		Body laserBody = PhysicsFactory.createBoxBody(GameScene.physicsWorld, mLaser, BodyType.KinematicBody, areaFixtureDef);
		laserBody.setUserData(mLaser);
		GameScene.physicsWorld.registerPhysicsConnector(new PhysicsConnector(
				mLaser, laserBody, true, false));
		//createContactListener();
		attachChild(mLaser);
	}
	
	public void fireBullet() {
		float bulletSpeed = 30f;
		float turretRotation = getRotation();
		Rectangle mBullet = new Rectangle(getX()-25, getY()+5, 25, 1.75f, mvbom);
		mBullet.setRotation(turretRotation);
		mBullet.setColor(Color.RED);
		FixtureDef bulletFixtureDef = PhysicsFactory.createFixtureDef(0, 0, 0);
		bulletFixtureDef.isSensor = true;
		Body mBulletBody = PhysicsFactory.createBoxBody(GameScene.physicsWorld, mBullet, BodyType.KinematicBody, bulletFixtureDef);
		mBulletBody.setUserData("bullet");
		GameScene.physicsWorld.registerPhysicsConnector(new PhysicsConnector(mBullet, mBulletBody, true, false));
		float xComponent = (float) (-Math.abs(Math.cos(Math.toRadians(turretRotation)))*bulletSpeed);
		float yComponent = (float) (Math.sin(Math.toRadians(turretRotation))*bulletSpeed);
		mBulletBody.setLinearVelocity(new Vector2(xComponent, yComponent));
		mEngine.getScene().attachChild(mBullet);
	}
	
	/*private ContactListener createContactListener()
	{
	    ContactListener contactListener = new ContactListener()
	    {
	        @Override
	        public void beginContact(Contact contact)
	        {
	            final Fixture x1 = contact.getFixtureA();
	            final Fixture x2 = contact.getFixtureB();
	            Log.d("X1 user data", (String) x1.getBody().getUserData());
	            Log.d("X2 user data", (String) x2.getBody().getUserData());
	            if(((x2.getBody().getUserData().equals("player")&&x1.getBody().getUserData().equals("laser"))||(x1.getBody().getUserData().equals("player")&&x2.getBody().getUserData().equals("laser")))&&isInsideZone==false)
	            {
	                isInsideZone = true;
	                fireBullet();
	            }
	        }
	 
	        @Override
	        public void endContact(Contact contact)
	        {
	            final Fixture x1 = contact.getFixtureA();
	            final Fixture x2 = contact.getFixtureB();
	           
	            if(((x2.getBody().getUserData().equals("player")&&x1.getBody().getUserData().equals("laser"))||(x1.getBody().getUserData().equals("player")&&x2.getBody().getUserData().equals("laser")))&&isInsideZone==true)
	            {
	                isInsideZone = false;
	                //action on exit
	            }
	        }
	 
	        @Override
	        public void preSolve(Contact contact, Manifold oldManifold)
	        {
	               
	        }
	 
	        @Override
	        public void postSolve(Contact contact, ContactImpulse impulse)
	        {
	               
	        }
	    };
	    return contactListener;
	}*/
}
